//
//  LGTabbarController.m
//  微博
//
//  Created by MS on 15-12-3.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "LGTabbarController.h"
#import "UIImage+Image.h"
#import "LGTabBar.h"
#import "LGHomeController.h"
#import "LGDiscoverController.h"
#import "LGMessageController.h"
#import "LGProfileController.h"
#import "LGNavigationController.h"
#import "LGUserTool.h"
#import "LGUserResult.h"
#import "LGComposeController.h"
@interface LGTabbarController ()<LGtabBarDelegate>

@property (nonatomic,strong) NSMutableArray * items;

@property (nonatomic,weak) LGHomeController * home;
@property (nonatomic,weak) LGMessageController * message;
@property (nonatomic,weak) LGDiscoverController * discover;
@property (nonatomic,weak) LGProfileController * profile;

@end

@implementation LGTabbarController

-(NSMutableArray *)items
{
    if (!_items) {
        
        _items = [NSMutableArray array];
 
    }
    return  _items;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUpAllChildViewController];
    
    //修改系统tabBar上的按钮
    [self setUpTabBar];
    
    // 每隔一段时间请求未读数
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(setUnreadNum) userInfo:nil repeats:YES];
}

#pragma mark 获取未读数
-(void)setUnreadNum
{
    
      // 请求微博的未读数
    [LGUserTool unreadWithSuccess:^(LGUserResult *result) {
        // 设置首页未读数
        _home.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",result.status];
        // 设置消息未读数
        _message.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",result.messageCount];
        // 设置我的未读数
        _profile.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",result.follower];
        // 设置应用程序所有的未读数
        [UIApplication sharedApplication].applicationIconBadgeNumber =result.totalCount;
        
        
    } failure:^(NSError *error) {
        
    }];
    
}

//TabBar在该方法中添加子控件

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    for (UIView * tabBaButton in self.tabBar.subviews) {
        if ([tabBaButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            [tabBaButton removeFromSuperview];
        }
    }
    
}


-(void)setUpTabBar
{
    LGTabBar * tabBar =[[LGTabBar alloc] initWithFrame:self.tabBar.bounds];
    tabBar.backgroundColor = [UIColor whiteColor];
    tabBar.delegate = self;
    
    tabBar.items = self.items;
    
    [self.tabBar addSubview:tabBar];
    

}



#pragma mark - 当点击tabBar上的按钮调用
-(void)tabBar:(LGTabBar *)tabBar didClickButton:(NSInteger)index
{
    if (index==0 && self.selectedIndex == 0) {// 点击首页，刷新
        [_home refresh];
    }
    
    //切换控制器
    self.selectedIndex = index;
    
}

-(void)tabBardidClickPluseButton:(LGTabBar *)tabBar
{
    LGComposeController * composeVc = [[LGComposeController alloc] init];
    LGNavigationController * nav = [[LGNavigationController alloc] initWithRootViewController:composeVc];
    [self presentViewController:nav animated:YES completion:nil];
    
}

#pragma 设置所有子控制器
-(void) setUpAllChildViewController
{
    //首页
    LGHomeController * home =[[LGHomeController alloc] init];
    [self setUpOneController:home andImage:[UIImage imageWithOriginalName:@"tabbar_home"] andSelectImage:[UIImage imageWithOriginalName:@"tabbar_home_selected"] andTabbarItemTitle:@"首页"];
    _home = home;
   // home.view.backgroundColor = [UIColor whiteColor];
    
    
    //消息
    LGMessageController * message =[[LGMessageController alloc] init];
    [self setUpOneController:message andImage:[UIImage imageWithOriginalName:@"tabbar_message_center"] andSelectImage:[UIImage imageWithOriginalName:@"tabbar_message_center_selected"] andTabbarItemTitle:@"消息"];
    _message = message;
  //  message.view.backgroundColor = [UIColor greenColor];

    //发现
    LGDiscoverController * discover =[[LGDiscoverController alloc] init];
    [self setUpOneController:discover andImage:[UIImage imageWithOriginalName:@"tabbar_discover"] andSelectImage:[UIImage imageWithOriginalName:@"tabbar_discover_selected"] andTabbarItemTitle:@"发现"];
    _discover = discover;
   //  discover.view.backgroundColor = [UIColor redColor];
    
    //我的
    LGProfileController * profile =[[LGProfileController alloc] init];
    [self setUpOneController:profile andImage:[UIImage imageWithOriginalName:@"tabbar_profile"] andSelectImage:[UIImage imageWithOriginalName:@"tabbar_profile_selected"] andTabbarItemTitle:@"我的"];
    _profile = profile;
  //  profile.view.backgroundColor = [UIColor lightGrayColor];

}

//
#pragma mark 设置一个子控件
-(void) setUpOneController:(UIViewController *)vc  andImage:(UIImage *)image andSelectImage:(UIImage *)selectImage andTabbarItemTitle:(NSString *)title
{

    vc.title = title;
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectImage;

    [self.items addObject:vc.tabBarItem];
    
    LGNavigationController * nav = [[LGNavigationController alloc] initWithRootViewController:vc];
    
    
    [self addChildViewController:nav];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
